package com.xmmj.main.viewmodel

import android.view.View
import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableBoolean
import androidx.databinding.ObservableList
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.xmmj.baselibrary.base.activity.mvvm.BaseViewModel
import com.xmmj.baselibrary.base.activity.mvvm.ItemViewModel
import com.xmmj.baselibrary.base.activity.mvvm.OnItemClickListener
import com.xmmj.common.router.RouteNavigator
import com.xmmj.main.model.MainModel
import com.xmmj.main.viewmodel.itemviewmodel.VideoItemViewModel
import com.xmmj.module_main.BR
import com.xmmj.module_main.R
import me.tatarka.bindingcollectionadapter2.OnItemBind

class MainViewModel : BaseViewModel<MainModel>() {
    //页码
    var page = 1

    //每页个数
    var pageSize = 20

    //分类
    var category = 1

    //显示空提示
    var showEmpty = ObservableBoolean(false)

    //点击登录事件
    var clickLogin = View.OnClickListener {
        RouteNavigator.routeLogin()
    }

    //点击注册事件
    var clickRegister = View.OnClickListener {
        toast("暂未实现")
    }

    //item的数据集
    var items: ObservableList<VideoItemViewModel> = ObservableArrayList()

    //绑定item布局
    var itemBind = OnItemBind<VideoItemViewModel> { itemBinding, position, item ->
        itemBinding.set(BR.itemViewModel, R.layout.item_video)
            .bindExtra(BR.itemClickListener, object : OnItemClickListener {

                override fun onItemClick(vm: ItemViewModel<*>?) {
                    toast("点击第${position}个item")
                }
            })
    }

    //刷新和加载更多的监听器
    var onRefreshLoadMoreListener: OnRefreshLoadMoreListener =
        object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                getVideo(showLoading = false, refresh = true)
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                getVideo(showLoading = false, refresh = false)
            }
        }

    /**
     * 获取视频列表数据
     */
    fun getVideo(showLoading: Boolean, refresh: Boolean) {
        launch {
            //显示Loading
            if (showLoading) {
                showLoading(true)
            }

            //重载页面
            page = if (refresh) 1 else page

            //请求视频数据
            val video = mModel!!.getVideos(page, pageSize, category)

            //如果是刷新状态，先清空数据集
            if (refresh) {
                items.clear()
            }

            //添加数据集
            if (video.isNotEmpty()) {
                video.forEach {
                    items.add(VideoItemViewModel(this@MainViewModel, it))
                }
                page++
            }

            //隐藏Loading
            hideLoading()
            //结束刷新动画
            finishRefresh()
            //结束加载更多动画
            finishLoadMore(video.isEmpty())
            //显示/隐藏空提示
            showEmpty.set(items.size <= 0)
        }
    }
}